//
//  CustomMessageModel.m
//  FFMall
//
//  Created by ale tan on 2017/9/26.
//  Copyright © 2017年 IT.Bear. All rights reserved.
//

#import "CustomMessageModel.h"
#import "NSString+TitleSize.h"
#import "FFTools.h"

@implementation CustomMessageModel

+ (void)sendMessageToService:(JMSGMessage *)message{
//    FFSendMessage_VM *sendMessageVM = [[FFSendMessage_VM alloc]init];
//    [sendMessageVM sendMessageWithMessage:message CallBack:^(id responseDic, BOOL result) {
//
//    }];
}

+ (CGFloat)cellHeightWithMessage:(JMSGMessage *)message{
    JMSGCustomContent *content = (JMSGCustomContent *)message.content;
    NSInteger customMessageType = [content.customDictionary[@"type"] integerValue];
    switch (customMessageType) {
            //文字
        case 1:
        {
            NSString *chatText = content.customDictionary[@"content"];
            CGFloat textHeight = [chatText sizeWithStringFont:APP_FONT_PINGFANG_SC(15) constrainedToSize:CGSizeMake(IPHONE_WIDTH-HeaderToEdge-ContentToEdge-HeaderToBubble-HeaderWidth-ContentToBubbleHead-ContentToBubble, 0) lineSpacing:0].height;
            return textHeight>HeaderHeight? textHeight+ TopEdge*2:HeaderHeight+TopEdge*2;
        }
            break;
            
            //图片
        case 2:
        {
            return 100;
        }
            break;
            
            //商品（图文混排）
        case 3:
        {
            return ProductCellHeight+TopEdge*2;;
        }
            break;
            
            //订单（图文混排）
        case 4:case 6:
        {
            NSDictionary *dic = content.customDictionary[@"content"];
            if ([dic isKindOfClass:[NSString class]]) {
                NSString *jsonStr = content.customDictionary[@"content"];
                dic = [FFTools toArrayOrNSDictionary:jsonStr];
            }
            FBOrderModel *orderModel = [[FBOrderModel alloc]initWithDictionary:dic];
            NSInteger row = orderModel.products.count>2?2:orderModel.products.count;
            return ProductCellHeight*row+TopEdge*2+44;
        }
            break;
            
            //询图
//        case 5:
//        {
//            NSDictionary *dic = content.customDictionary[@"content"];
//            if ([dic isKindOfClass:[NSString class]]) {
//                NSString *jsonStr = content.customDictionary[@"content"];
//                dic = [FFTools toArrayOrNSDictionary:jsonStr];
//            }
//
//            FFImageConsultModel *consultModel = [[FFImageConsultModel alloc]initWithDictionary:dic];
//
//            //显示几个商品
//            NSArray *products = consultModel.match_products;
//            if (!products || !products.count) {
//                products = consultModel.consult_products;
//            }
//
//            CGFloat height = products.count>2?ProductCellHeight*2+35:ProductCellHeight*products.count;
//
//            //询图号和询图状态
//            height += 44*2;
//
//            //根据状态判断有没有按钮
//            NSInteger status = [consultModel.consult_status integerValue];
//            if (status==2||status==3||status==4) {
//                height+=44;
//            }
//
//            return height+TopEdge*2;
//        }
//            return 0;
//            break;
            
            //其他（未定义）
        default:
        {
            return TopEdge*2;
        }
            break;
    }
}

+ (CustomMessageBaseCell *)cellWithMessage:(JMSGMessage *)message InTableView:(UITableView *)tableView{
    JMSGCustomContent *content = (JMSGCustomContent *)message.content;
    NSString *type = content.customDictionary[@"type"];
    
    switch ([type integerValue]) {
        case 1:
            {
                CustomMessageTextCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
                if (!cell) {
                    cell = [[CustomMessageTextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
                }
                
                return cell;
            }
            break;
            
        case 2:
        {
            CustomMessageImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
            if (!cell) {
                cell = [[CustomMessageImageCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
            }
            
            return cell;
        }
            break;
            
        case 3:case 4:
        {
            CustomMessageCoreCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
            if (!cell) {
                cell = [[CustomMessageCoreCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
            }
            
            return cell;
        }
            break;
            
//        case 5:
//        {
//            CustomMessageConsultCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
//            if (!cell) {
//                cell = [[CustomMessageConsultCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
//            }
//
//            return cell;
//        }
//            break;
            
        default:
        {
            CustomMessageBaseCell *cell = [tableView dequeueReusableCellWithIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
            if (!cell) {
                cell = [[CustomMessageBaseCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellReuseIDWithSenderAndType(message.isReceived?@"1":@"0", type)];
            }
            
            return cell;
        }
            break;
    }
}

@end
